package utils

import (
	"fmt"
	"github.com/kataras/iris/v12"
	"log"
	"os"
	"sync"
	"time"

	"gopkg.in/yaml.v2"
)

var (
	ServerConf   *Yaml
	BaseYamlPath string
	IrisPath     string
	LocationZone *time.Location
	VerifySign   bool
)

var Loc, _ = time.LoadLocation("Asia/Shanghai")
var Md5Key = "taskAssignmentSystem"
var MaxCoverSize = 5 * iris.MB

func ServiceString() string {
	return fmt.Sprintf("%s:%s", ServerConf.Server.Host, ServerConf.Server.Port)
}

func loadYaml() {
	LocationZone, _ = time.LoadLocation("Asia/Shanghai")
	switch os.Getenv("Env") {
	case "Develop":
		BaseYamlPath = "./conf.yaml"
		IrisPath = "./iris.yaml"
	default:
		BaseYamlPath = "/Users/zxcs/Desktop/tf/任务/task_assignment_api/config/conf.yaml"
		IrisPath = "/Users/zxcs/Desktop/tf/任务/task_assignment_api/config/iris.yaml"

	}
	yamlFile, err := os.ReadFile(BaseYamlPath)
	if err != nil {
		log.Fatal(err)
	} else {
		err = yaml.Unmarshal(yamlFile, &ServerConf)
		if err != nil {
			log.Fatal(err)
		}
	}
	VerifySign = ServerConf.VerifySign
}

var loadYamlOnce sync.Once

func init() {
	loadYamlOnce.Do(loadYaml)
}
